﻿using DingVision.Axis.Models;
using DingVision.Infrastructure.Events;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DingVision.Axis.ViewModels
{
    public class IOPageViewModel : BindableBase
    {
        #region 变量
        private readonly IEventAggregator eventAggregator;
        #endregion
        #region 属性绑定
        private ObservableCollection<bool> dMC3600Di = new ObservableCollection<bool>();
        public ObservableCollection<bool> DMC3600Di
        {
            get { return dMC3600Di; }
            set { SetProperty(ref dMC3600Di, value); }
        }
        private ObservableCollection<bool> dMC3600Do = new ObservableCollection<bool>();
        public ObservableCollection<bool> DMC3600Do
        {
            get { return dMC3600Do; }
            set { SetProperty(ref dMC3600Do, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand<object> outActionCommand;
        

        public DelegateCommand<object> OutActionCommand =>
            outActionCommand ?? (outActionCommand = new DelegateCommand<object>(ExecuteOutActionCommand));

        void ExecuteOutActionCommand(object obj)
        {
            if (DmcCard.Instance.OpenCardOk)
            {
                ushort bitno = ushort.Parse(obj.ToString());
                DmcCard.Instance.SetDo(bitno, (ushort)(DMC3600Do[bitno] ? 0 : 1));
            }
      
        }
        #endregion
        #region 构造函数
        public IOPageViewModel(IEventAggregator _eventAggregator)
        {
            for (int i = 0; i < 16; i++)
            {
                DMC3600Di.Add(false);
            }
            for (int i = 0; i < 16; i++)
            {
                DMC3600Do.Add(false);
            }
            eventAggregator = _eventAggregator;
            eventAggregator.GetEvent<ShellSendEvent>().Subscribe(ShellSendEventReceived);
        }


        #endregion
        #region 自定义函数
        private void ShellSendEventReceived(string msg)
        {
            switch (msg)
            {
                case "Loaded":
                    Run();
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 运行
        async void Run()
        {
            while (true)
            {
                try
                {
                    if (DmcCard.Instance.OpenCardOk)
                    {
                        uint diPort = DmcCard.Instance.GetDiPort();
                        uint doPort = DmcCard.Instance.GetDoPort();
                        for (int i = 0; i < 16; i++)
                        {
                            DMC3600Di[i] = ((ulong)diPort & (ulong)((long)((int)Math.Pow(2.0, (double)i)))) == 0UL;
                            DMC3600Do[i] = ((ulong)doPort & (ulong)((long)((int)Math.Pow(2.0, (double)i)))) == 0UL;

                        }
                    }
                }
                catch { }
                await Task.Delay(100);
            }

        }
        #endregion
    }
}
